<?php

namespace addons\shopro\controller;

use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\Withdraw as WithdrawModel;
use addons\shopro\service\Withdraw as WithdrawLibrary;
use app\common\model\GoldenLog;
use app\common\model\shopro\WithdrawGoldenModel;
use app\common\model\User;
use app\common\model\Userchildren;
use think\Db;
use app\common\library\Ems;
class Withdraw extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $user      = auth_user();
        $withdraws = WithdrawModel::where(['user_id' => $user->id])->order('id desc')->paginate($this->request->param('list_rows',
            10))->each(function ($withdraw) {
            $withdraw->hidden(['withdraw_info']);
        });

        $this->success('获取成功', $withdraws);
    }

    public function golend_log(){
         $user      = auth_user();
        $withdraws = WithdrawGoldenModel::where(['user_id' => $user->id])->order('id desc')->paginate($this->request->param('list_rows',
            10))->each(function ($withdraw) {
        });

        $this->success('获取成功', $withdraws);
    }

    // 提现规则
    public function rules()
    {
        $user   = auth_user();
        $config = (new WithdrawLibrary($user))->config;

        $this->success('提现规则', $config);
    }


    // 发起提现请求
    public function apply()
    {
        $user   = auth_user();
        if($user->is_frozen == 1 || $user->is_lock == 1){
            $this->error('账户已被冻结，请联系工作人员');
        }
        $params = $this->request->param();
        $this->svalidate($params, ".apply");
//        $email = input('email');
//        $code  = input('code');
//        if(!$email || !$code){
//            $this->error('请填写邮箱和验证码');
//        }
        $params['firepower'] = 0;
        if($user->is_firepower == 1){
            $firepower = sheep_config('shop.recharge_withdraw.withdraw.firepower');
            if($firepower > 0){
                $params['firepower'] = bcmul($params['money'],$firepower/100,2);
                if($user->firepower < $params['firepower']){
                    $this->error('贡献值不够');
                }
            }
        }

//        if($email != $this->auth->email){
//            $this->error('请输入自己的邮箱');
//        }

//       if(!Ems::check($email,$code,'withdraw_apply')){
//           $this->error('邮箱验证码错误');
//       }

        $withdrawLib = new WithdrawLibrary($user);
        $withdraw    = $withdrawLib->apply($params);

        $this->success('申请成功', $withdraw);
    }

    /**
     * @menu:金豆提现
     * @Author:男人的坏
     * @Date:2024-04-14 18:54
     */
    public function golden_apply()
    {
        $user   = auth_user();
        $money = input('post.money', 0);
        if ($money <= 0) {
            $this->error('请输入正确金豆');
        }
        $sxf        = bcmul($money, config('site.golden_sxf') / 100,2);
        $userGolden = $user->golden;
        if ($sxf) {
            $userGolden -= $sxf;
        }
        if ($userGolden < $money) {
            $this->error('你没有那么多金豆可提现');
        }

        $withdraw = Db::transaction(function () use ($money, $sxf,$user) {
            $model              = new WithdrawGoldenModel();
            $model->user_id     = $user->id;
            $model->charge_fee  = $sxf;
            $model->withdraw_sn = get_sn($user->id, 'G');
            $model->platform    = request()->header('platform');
            $model->amount      = $money;
            $model->paid_fee    = $money - $sxf;
            $model->save();
            return WalletService::change($user->id, 'golden', -$money, 'golden_apply', [],
                '金豆提现');
        });
        if($withdraw){
            $this->success('申请成功，等待后台审核');
        }
        $this->error('提现失败');
    }

    /**
     * @menu:金豆转让
     * @Author:男人的坏
     * @Date:2024-04-14 21:41
     */

    public function golden_turn(){
        $config = sheep_config('shop.recharge_withdraw.withdraw');
        if($config['auto_arrival'] == 1){
            $this->error('暂无法转账');
        }
        $mobile = input('post.mobile');
        $secure_password    = input('secure_password');
        if(!$mobile || !$secure_password){
            $this->error('缺少参数');
        }
        if($this->auth->secure_password != $this->auth->getEncryptPassword($secure_password, $this->auth->salt)){
            $this->error('安全密码不对');
        }
        $user   = auth_user();
        $money = input('post.money', 0);
        if ($money <= 0) {
            $this->error('请输入正确金豆');
        }
        $twoUserInfo = User::where(['mobile'=>$mobile])->find();
        if(!$twoUserInfo){
            $this->error('用户不存在');
        }
        $userchildrenModel = new Userchildren();
        $teshu = $userchildrenModel->where(['member_id'=>761,'children_id'=>$this->auth->id])->find();
        if($teshu){
             if($twoUserInfo['id'] =! 761){
                 $this->error('不能个该用户转');
             }
        }else{
            if($this->auth->is_manager  == 1){
                $where = ['member_id'=>$this->auth->id,'children_id'=>$twoUserInfo['id']];
            }else{
                if($this->auth->mobile == $mobile){
                    $this->error('不能给自己转');
                }
                $where = ['member_id'=>$twoUserInfo['id'],'children_id'=>$this->auth->id];
                if($twoUserInfo['is_manager'] !=1){
                    $this->error('不能给该用户转');
                }
            }
            $userchildren = $userchildrenModel->where($where)->find();
            if(!$userchildren){
                $this->error('不能为该用户转');
            }
        }
        if ($user->golden < $money) {
            $this->error('你没有那么多金豆可提现');
        }
        $withdraw = Db::transaction(function () use ($money,$user,$twoUserInfo) {

            $withdraw =  WalletService::change($user->id, 'golden', -$money, 'golden_turn', [],
                '金豆互转');//扣除金豆
            $withdraw =  WalletService::change($twoUserInfo->id, 'gold', $money, 'gold_turn', [],
                '金豆互转');//增加金币
            return $withdraw;
        });
        if($withdraw){
            $this->success('转让成功');
        }
        $this->error('转让失败');
    }

    /**
     * @menu 金币互转
     * @Author:男人的坏
     * @Date:2024-04-22 13:58
     */
    public function gold_turn(){
//        $config = sheep_config('shop.recharge_withdraw.withdraw');
//        if($config['auto_arrival'] == 1){
//            $this->error('暂无法转账');
//        }
        $mobile = input('post.mobile');
        $secure_password    = input('secure_password');
        if(!$mobile || !$secure_password){
            $this->error('缺少参数');
        }

        $user   = auth_user();
        if($user->secure_password != $this->auth->getEncryptPassword($secure_password, $user->salt)){
            $this->error('安全密码不对');
        }
        $money = input('post.money', 0);
        if ($money <= 0) {
            $this->error('请输入正确金币');
        }
        if($user->gold < $money){
            $this->error('可转金币不足');
        }
        $twoUserInfo = User::where(['id'=>['neq',$user->id],'mobile'=>$mobile])->find();
        if(!$twoUserInfo){
            $this->error('用户不存在');
        }

        $withdraw = Db::transaction(function () use ($money,$user,$twoUserInfo) {
            $withdraw =  WalletService::change($user->id, 'gold', -$money, 'gold_turn', [],
                '用户'.$user->id.'金币赠予用户'.$twoUserInfo->id);//扣除金币
            $withdraw =  WalletService::change($twoUserInfo->id, 'gold', $money, 'gold_receive', [],
                '用户'.$user->id.'金币赠予用户'.$twoUserInfo->id);//增加金币
            return $withdraw;
        });
        if($withdraw){
            $this->success('转赠成功');
        }
        $this->error('转让失败');
    }

}
